class Semester < ActiveRecord::Base
  include Comparable
  has_many  :students,  :through => :semester_registration
  has_many :registrations, :class_name => "SemesterRegistration", :foreign_key => "semester_id"
  has_many  :courses,   :through => :future_course
  has_many :sections
  
  has_many  :futures, :through => 'futures'
  has_many  :pasts,   :through => 'pasts'
  
  def self.current_and_future
    self.find(:all).select { |e| e.end_date >= Date.today }.sort { |a, b| a.start_date <=> b.start_date }
  end
  
  def self.with_name(name)
    Semester.find(:all).select { |e| e.name == name }[0]
  end
  
  def name
    return self.season.downcase + "_" + self.start_date.year.to_s
  end
  
  def <=>(other)
    return self.start_date <=> other.start_date
  end
  
  def element_id
    self.name + '_courses'
  end
end
